package com.sanss.trapParse.xml;

import java.util.*;
import java.io.*;
import org.dom4j.io.*;
import org.dom4j.*;

import com.sanss.trapParse.info.MibInfo;

public class XmlParser {

	String OID;

	public XmlParser(String OID) {
		this.OID = OID.substring(0, OID.length() - 2);
	}

	public MibInfo Analysis(InputStream in) {
		MibInfo mi = new MibInfo();
		mi.setOID(OID);
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(in);
			// find element with given oid
			Element OIDinfo = (Element) doc.selectSingleNode("//*[@oid=\""
					+ OID + "\"]");
			if (OIDinfo == null)
				return null;// object not found
			else {
				// parse mib into object
				mi.setName(OIDinfo.attributeValue("name"));
				mi.setStatus(OIDinfo.attributeValue("status"));
				mi.setAccess(OIDinfo.elementText("access"));
				mi.setDescription(OIDinfo.elementTextTrim("description"));
				mi.setSyntax(parseSyntax(OIDinfo));
				mi.setObject(parseObject(OIDinfo));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mi;
	}

	// parse syntax
	private String parseSyntax(Element e) {
		StringBuilder syntaxString = new StringBuilder("");
		// String syntaxString = "";
		String tmp;
		// check element
		Element syntax = e.element("syntax");
		if (syntax == null)
			return null;

		if (syntax.elements("typedef").size() > 0) {// typedef
			Element typedef = syntax.element("typedef");
			syntaxString.append("basetype=")
					.append(typedef.attributeValue("basetype")).append(";");
			// syntaxString += "basetype=" + typedef.attributeValue("basetype")
			// + ";";
			if (typedef.elements("namednumber").size() > 0) {// namednumber
				for (Iterator j = typedef.elementIterator("namednumber"); j
						.hasNext();) {
					Element namednumber = (Element) j.next();
					tmp = namednumber.attributeValue("name") + "="
							+ namednumber.attributeValue("number") + ";";
					syntaxString.append(tmp);
					// syntaxString += tmp;
				}
				return syntaxString.toString();
			} else if (typedef.elements("range").size() > 0) {// range
				for (Iterator j = typedef.elementIterator("range"); j.hasNext();) {
					Element range = (Element) j.next();
					tmp = "min=" + range.attributeValue("min") + ";" + "max="
							+ range.attributeValue("max") + ";";
					syntaxString.append(tmp);
					// syntaxString += tmp;
				}
				return syntaxString.toString();
			}
		} else if (syntax.elements("type").size() > 0) {// typedef
			Element type = syntax.element("type");
			tmp = "module=" + type.attributeValue("module") + ";" + "name="
					+ type.attributeValue("name") + ";";
			syntaxString.append(tmp);
			// syntaxString += tmp;
			return syntaxString.toString();
		}
		return null;
	}

	// parse object
	private String parseObject(Element e) {
		StringBuilder objectString = new StringBuilder("");
		// String objectString = "";
		String tmp;
		if (e.elements("objects").size() > 0) {
			Element objects = e.element("objects");
			for (Iterator j = objects.elementIterator("object"); j.hasNext();) {
				Element obj = (Element) j.next();
				tmp = "module=" + obj.attributeValue("module") + ";" + "name="
						+ obj.attributeValue("name") + ";";
				objectString.append(tmp);
				// objectString += tmp;
			}
			return objectString.toString();
		}
		return null;
	}

}
